package com.sky.config;

import com.sky.context.BaseContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 从请求头获取token
        String token = request.getHeader("Authorization");
        // 解析token（示例：假设token包含用户ID，实际需结合JWT等工具解析）
        Long userId = parseTokenGetUserId(token);
        if (userId != null) {
            BaseContext.setCurrentId(userId); // 设置到BaseContext
            return true;
        }
        // 未登录，可直接返回401或抛异常
        response.setStatus(401);
        return false;
    }

    private Long parseTokenGetUserId(String token) {
        // 实际需用JWT等工具解析，这里简化示例
        // 比如：return JwtUtil.parseToken(token).getUserId();
        return 123L; // 伪代码，替换为真实解析逻辑
    }
}